#!/bin/bash
set -e

export FORCE_COLOR=1

if [ -x ".env" ]; then
  set -a
  . ./.env
  set +a
fi;

SERVER_VERSION_NUM=$(psql -XAqtv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "show server_version_num;")

psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "drop database graphilecrystaltest;" || true
for DBNAME in $(psql -XAqtv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "select datname from pg_database where datname like 'gctestdb_%' and datname = format('%I', datname);"); do
  echo "DEL $DBNAME"
  psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "drop database $DBNAME;"
done
psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c 'alter database graphilecrystaltest_template with is_template = false;' || true
psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c 'drop database graphilecrystaltest_template;' || true
psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c 'drop user graphilecrystaltest;' || true
psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "create user graphilecrystaltest with superuser login password 'test';"
psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "create database graphilecrystaltest_template with owner graphilecrystaltest;"

psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" <<HERE
-- Reset the roles
drop role if exists postgraphile_test_authenticator;
drop role if exists postgraphile_test_user1;
drop role if exists postgraphile_test_user2;
drop role if exists postgraphile_test_visitor;
create role postgraphile_test_authenticator noinherit login password 'testpassword';
create role postgraphile_test_visitor noinherit;
create role postgraphile_test_user1;
create role postgraphile_test_user2;
grant postgraphile_test_visitor to postgraphile_test_user1;
grant postgraphile_test_visitor to postgraphile_test_user2;
grant postgraphile_test_user1 to postgraphile_test_authenticator;
grant postgraphile_test_user2 to postgraphile_test_authenticator;
grant postgraphile_test_authenticator to graphilecrystaltest;
HERE

PGPASSWORD=test psql -Xqv ON_ERROR_STOP=1 -d "graphilecrystaltest_template" -U graphilecrystaltest \
  -f grafast/dataplan-pg/__tests__/schema.sql \
  -f grafast/dataplan-pg/__tests__/sequence_reset.sql \
  -f postgraphile/postgraphile/__tests__/kitchen-sink-schema.sql \
  -f postgraphile/postgraphile/__tests__/kitchen-sink-permissions.sql \
  -f postgraphile/postgraphile/__tests__/kitchen-sink-data.sql \
  -f postgraphile/postgraphile/__tests__/pg11-schema.sql \
  -f postgraphile/postgraphile/__tests__/pg11-data.sql \
  -f graphile-build/graphile-utils/__tests__/utils-schema.sql
#if [ $SERVER_VERSION_NUM -ge 100000 ]; then
#  psql -Xqv ON_ERROR_STOP=1 -d "graphilecrystaltest_template" -f postgraphile/postgraphile/__tests__/pg10-data.sql
#fi;

#psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" <<HERE
#DROP DATABASE IF EXISTS lds_test;
#CREATE DATABASE lds_test;
#HERE
#psql -X1v ON_ERROR_STOP=1 -v VERBOSITY=verbose -d lds_test -f utils/lds/__tests__/schema.sql

psql -Xqv ON_ERROR_STOP=1 -d "${OWNER_DATABASE:-postgres}" -c "create database graphilecrystaltest with template = graphilecrystaltest_template owner = graphilecrystaltest;"

clear
echo "Database reset successfully ✅"
